//
//  NativeViewController.m
//  Runner
//
//  Created by huimac on 2021/10/27.
//

#import "NativeViewController.h"
#import "SyncDatamanager.h"

@interface NativeViewController ()
@property (nonatomic, strong) UILabel *label1, *label2;
@end

@implementation NativeViewController {
    NSString *_model1_lisnten_id1, *_model1_lisnten_id2,
    *_model2_lisnten_id1, *_model2_lisnten_id2;
}

- (void)viewDidLoad {
    [super viewDidLoad];

    
    __weak typeof(self) weakSelf = self;
    _model1_lisnten_id1 = [SyncDatamanager.getModel1 addListener:^{
        weakSelf.label1.text = SyncDatamanager.getModel1.name;
    }];
    _model1_lisnten_id2 = [SyncDatamanager.getModel1 addListener:^{
        NSLog(@"model1 did receive change2");
    }];
    
    _model2_lisnten_id1 = [SyncDatamanager.getModel2 addListener:^{
        weakSelf.label2.text = SyncDatamanager.getModel2.name;
    }];
    _model2_lisnten_id2 = [SyncDatamanager.getModel2 addListener:^{
        NSLog(@"model2 did receive change2");
    }];
    
    
    self.label1.text = SyncDatamanager.getModel1.name;
    {
        UIButton *button = [[UIButton alloc] initWithFrame:CGRectMake(100, 100, 100, 50)];
        [button setTitle:@"随机修改1" forState:UIControlStateNormal];
        button.backgroundColor = [UIColor redColor];
        [self.view addSubview:button];
        [button addTarget:self action:@selector(buttonAction) forControlEvents:UIControlEventTouchUpInside];
    }
    
    self.label2.text = SyncDatamanager.getModel2.name;
    {
        UIButton *button = [[UIButton alloc] initWithFrame:CGRectMake(220, 100, 100, 50)];
        [button setTitle:@"随机修改2" forState:UIControlStateNormal];
        button.backgroundColor = [UIColor redColor];
        [self.view addSubview:button];
        [button addTarget:self action:@selector(buttonAction2) forControlEvents:UIControlEventTouchUpInside];
    }
    
}


-(void)buttonAction {
    SyncDatamanager.getModel1.name = [NSString stringWithFormat:@"fix_model_name:%u",arc4random()%10000];
    [SyncDatamanager.getModel1 notifyListeners:^{
        NSLog(@"native====notifyListeners_model1");
    }];
}

-(void)buttonAction2 {
    SyncDatamanager.getModel2.name = [NSString stringWithFormat:@"fix_model_name:%u",arc4random()%10000];
    [SyncDatamanager.getModel2 notifyListeners:^{
        NSLog(@"native====notifyListeners_model2");
    }];
}

- (void)dealloc {
    [SyncDatamanager.getModel1 removeListener:_model1_lisnten_id1];
    [SyncDatamanager.getModel1 removeListener:_model1_lisnten_id2];
    [SyncDatamanager.getModel2 removeListener:_model2_lisnten_id1];
    [SyncDatamanager.getModel2 removeListener:_model2_lisnten_id2];
}

-(UILabel *)label1 {
    if (_label1 == nil) {
        _label1 = [[UILabel alloc] initWithFrame:CGRectMake(20, 200, 300, 100)];
        _label1.backgroundColor = [UIColor grayColor];
        [self.view addSubview:_label1];
    }
    return _label1;
}

-(UILabel *)label2 {
    if (_label2 == nil) {
        _label2 = [[UILabel alloc] initWithFrame:CGRectMake(20, 350, 300, 100)];
        _label2.backgroundColor = [UIColor grayColor];
        [self.view addSubview:_label2];
    }
    return _label2;
}
@end
